﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OBUCare.Core.Models;
using OBUCare.Core.DataAccess;
using OBUCare.Core.Common; 

namespace OBUCare.Core.BL
{
    /// <summary>
    /// 和配置相关的方法
    /// </summary>
    public class ConfigMgr:IDisposable
    {
        private OBU_CAREContext obuDB = new OBU_CAREContext();
        private ISSUE_RPTContext issDB = new ISSUE_RPTContext();


        /// <summary>
        /// 获取速通编号
        /// </summary>
        /// <returns></returns>
        public AgentInfo GetCenterAgentNo()
        {
            return issDB.GetCenterAgentNo();
        }



        #region 配置项相关
        
        /// <summary>
        /// 根据配置值对应的配置表编码及配置值编码返回配置项对象
        /// </summary>
        /// <param name="typeCode">配置表编码</param>
        /// <param name="Valuecode">配置值编码</param>
        /// <returns></returns>
        public DICT_TYPE_VALUE GetTypeValue(string typeCode,string valueCode)
        {
            //string sql =string.Format("SELECT *  FROM [DICT_TYPE_VALUE] where DICT_CODE='{0}'",code);
            //return obuDB.Database.SqlQuery<DICT_TYPE_VALUE>(sql).FirstOrDefault();
            var result = from v in obuDB.DICT_TYPE_VALUE.Include("DICT_TYPE")
                         where v.DICT_TYPE.TYPE_CODE == typeCode && v.DICT_CODE == valueCode
                         select v;
            return result.FirstOrDefault();
        }

        /// <summary>
        /// 根据配置值的ID返回配置值
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public DICT_TYPE_VALUE GetTypeValue(int id)
        {
            return obuDB.DICT_TYPE_VALUE.Find(id);
        }

        /// <summary>
        /// 根据类型配置表的内部编码返回改配置表的所有配置值
        /// </summary>
        /// <param name="typeCode"></param>
        /// <returns></returns>
        public IEnumerable<DICT_TYPE_VALUE> GetTypeValues(string typeCode)
        {
            //var result = from v in obuDB.DICT_TYPE_VALUE.Include("DICT_TYPE")
            //             where v.DICT_TYPE.TYPE_CODE == typeCode
            //             select v;
            //return result.ToList();

            var type=(from t in obuDB.DICT_TYPE
                       where t.TYPE_CODE==typeCode
                          select t).FirstOrDefault();
            if(type==null)
                return null;
            else
            {
                int typeID=type.ID;
                var result = from v in obuDB.DICT_TYPE_VALUE
                             where v.DICT_TYPE_ID == typeID && v.STATUS==0
                             select v;
                return result.ToList();
            }
        }

        /// <summary>
        /// 通过枚举植及对应的配置表内部编码，返回对应的配置信息对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumValue"></param>
        /// <param name="tblName"></param>
        /// <returns></returns>
        public DICT_TYPE_VALUE GetTypeValue<T>(T enumValue,string tblName)
            where T:struct
        {
            string code = enumValue.ToInt().ToText ();
            return this.GetTypeValue(tblName, code);
        }

        
        #endregion

        public IEnumerable<SUPPLIER> GetSuppliers()
        {
            return obuDB.SUPPLIERS;
        }

        public SUPPLIER GetSupplier(int id)
        {
            var info = obuDB.SUPPLIERS.Find(id);
            return info;
        }

       public IList<AgentInfo> GetAgents(string agentNo)
        {
            return issDB.GetAgents(agentNo);
        }

        public IList<ValueText> GetAgentTypes(string agentNo)
        {
            return issDB.GetAgentTypes(agentNo);
        }

        public IEnumerable<OPERATOR> GetOPERATORS()
        {
            return issDB.GetOPERATORS();
        }

        public IEnumerable<OPERATOR> GetOPERATORSByAgentNo(string AgentNo)
        {
            return issDB.GetOPERATORSByAgentNo(AgentNo);
        }

        /// <summary>
        /// 获取下拉列表时的型号配置
        /// </summary>
        /// <returns></returns>
        public IEnumerable<PARTS_SPECS> GetPartsSpecs()
        { 
            int status=DictTypeStatus.Normal.ToInt().Value;
            var r = from s in obuDB.PARTS_SPECS
                    where s.STATUS == status
                    select s;
            return r;
        }
        /// <summary>
        /// 获取下拉列表时的型号配置
        /// </summary>
        /// <returns></returns>
        public IEnumerable<PARTS_SPECS> GetPartsSpecs(int supplierID)
        {
            int status = DictTypeStatus.Normal.ToInt().Value;
            var r = from s in obuDB.PARTS_SPECS
                    where s.STATUS == status && s.SUPPLIER_ID==supplierID
                    select s;
            return r;
        }
        /// <summary>
        /// 根据厂商、元器件获取型号配置
        /// </summary>
        /// <returns></returns>
        public IEnumerable<PARTS_SPECS> GetPartsSpecs(int supplierID,int partsType)
        {
            int status = DictTypeStatus.Normal.ToInt().Value;
            var r = from s in obuDB.PARTS_SPECS
                    where s.STATUS == status && s.SUPPLIER_ID == supplierID && s.PARTS_ID==partsType
                    select s;
            return r;
        }

        /// <summary>
        /// 获取型号配置
        /// </summary>
        /// <returns></returns>
        public PARTS_SPECS GetPartsSpec(int id)
        {
            var info= obuDB.PARTS_SPECS.Find(id);
            return info;
        }


        //#region 维修单状态


        ///// <summary>
        ///// 根据维修单状态枚举，返回维修单状态对象
        ///// </summary>
        ///// <param name="v"></param>
        ///// <returns></returns>
        //public DICT_TYPE_VALUE GetRepairOrderStatus(RepairOrderStatus v)
        //{
        //    return this.GetTypeValue(v, ConfigMgr.tblRepairOrderStatus);
        //}

        ///// <summary>
        ///// 根据维修单ID,返回维修单状态
        ///// </summary>
        ///// <param name="v"></param>
        ///// <returns></returns>
        //public RepairOrderStatus GetRepairOrderStatus(int id)
        //{
        //    int code = this.GetTypeValue(id).DICT_CODE.ToInt().Value;
        //    return (RepairOrderStatus)code;
        //}
        //#endregion

        //#region 交接方向

        ///// <summary>
        ///// 交接单类别
        ///// </summary>
        ///// <param name="v"></param>
        ///// <returns></returns>
        //public DICT_TYPE_VALUE GetTransferType(TransferType v)
        //{
        //    return this.GetTypeValue(v, ConfigMgr.tblTransferBillType );
        //}

        ///// <summary>
        ///// 交接单类别
        ///// </summary>
        ///// <param name="v"></param>
        ///// <returns></returns>
        //public TransferType GetTransferType(int id)
        //{
        //    int code = this.GetTypeValue(id).DICT_CODE.ToInt().Value;
        //    return (TransferType)code;
        //}

        //#endregion

        //#region 交易类型

        //public DICT_TYPE_VALUE GetTransactionType(TransactionType v)
        //{
        //    return this.GetTypeValue(v, ConfigMgr.tblTransactionType );
        //}

        //public TransactionType GetTransactionType(int id)
        //{
        //    int code = this.GetTypeValue(id).DICT_CODE.ToInt().Value;
        //    return (TransactionType)code;
        //}

        
        //#endregion

        //#region 调整费用类型 

        //public DICT_TYPE_VALUE GetAdjustType(AdjustType v)
        //{
        //    return this.GetTypeValue(v, ConfigMgr.tblAdjustType);
        //}

        //public AdjustType GetAdjustType(int id)
        //{
        //    int code = this.GetTypeValue(id).DICT_CODE.ToInt().Value;
        //    return (AdjustType)code;
        //}

        //#endregion

        public void Dispose()
        {
            obuDB.Dispose();
            issDB.Dispose();
        }

        /// <summary>
        /// 返回全部配件类型
        /// </summary>
        /// <returns></returns>
        public IEnumerable<PARTS_TYPE> GetPartsType()
        {
            return obuDB.PARTS_TYPE;
        }

        public SUPPLIER GetSupplierByName(string name)
        {
            var allItems = obuDB.SUPPLIERS;
            var temp = (from s in allItems
                       where s.NAME.Equals(name)
                       select s).FirstOrDefault();
            if (temp == null)
            {
                temp = (from s in allItems
                       where s.SHORTNAME.Equals(name)
                       select s).FirstOrDefault();
            }
            return temp;
        }

        #region 所有系统内置的配置表其及部分配置项目

        /// <summary>
        /// 维修单状态
        /// </summary>
        public const string tblRepairOrderStatus = "RepairOrderStatus";

        /// <summary>
        /// 交接单状态
        /// </summary>
        public const string tblTransferBillStatus = "TransferBillStatus";

        /// <summary>
        /// 交接单类别
        /// </summary>
        public const string tblTransferBillType = "TransferBillType";

        /// <summary>
        /// 维修类别
        /// </summary>
        public const string tblRepairType = "RepairType";
        
        /// <summary>
        /// 资金来源
        /// </summary>
        public const string tblFundType = "FundType";
        
        /// <summary>
        /// 支票的dict_code 
        /// </summary>
        public const string tblFundType_Check = "1";

        /// <summary>
        /// POS机的dict_code 
        /// </summary>
        public const string tblFundType_Pos= "3";

        /// <summary>
        /// 故障类型
        /// </summary>
        public const string tblFaultType = "FaultType";

        /// <summary>
        /// 故障类型为其它的dict_code
        /// </summary>
        public const string tblFaultType_Other = "6";

        /// <summary>
        /// 交易类型
        /// </summary>
        public const string tblTransactionType = "TransactionType";

        /// <summary>
        /// 费用调整类别
        /// </summary>
        public const string tblAdjustType = "AdjustType";

        /// <summary>
        /// 发票类型
        /// </summary>
        public const string tblInvoiceType = "InvoiceType";

        /// <summary>
        /// 处置方式
        /// </summary>
        public const string tblDisposeType = "DisposeType";

        /// <summary>
        /// 是否维修
        /// </summary>
        public const string tblConfirmRepaired = "ConfirmRepaired";

        #endregion

        #region Excel相关的常量

        /// <summary>
        /// 序号列
        /// </summary>
        public const int SerialNoIndex = 0;

        public const int startFaultIndex = 3;
        public const int endFaultIndex = 17;
        public const int faultNameRowIndex = 3;
        public const int DISPOSE_TYPEIndex = 19; //厂商处理意见(是:表示能维修/否：表示无法维修/置换) 
        public const int IsRepaired = 20;   //客户确认是否维修
        public const int reMarkIndex = 21;
        /// <summary>
        /// 维修单号
        /// </summary>
        public const int repairNoIndex = 1;  
        public const int OBUIndex = 2;
        public const int startRowIndex = 4;
        
        public static readonly KeyValuePair<int, int> SupplierIndex = new KeyValuePair<int, int>(1, 1);
        public static readonly KeyValuePair<int, int> ReturnDateIndex = new KeyValuePair<int, int>(1, 20);

        public const string PriceFlag_Invalid = "0"; //故障点标志为0是表示非故障点

        public const int OtherPartIndex = 17; //其它故障点

        public const string OtherPartName = "其它";//其它故障点标题

        public const decimal OtherPartCost = 20; //其它故障的速通成本,速通报价将在厂商成本价上增加其自身成本

        #endregion

        #region 其它常量

        public const string RepairGroupNo = "11010001";

        /// <summary>
        /// 备胶编号
        /// </summary>
        public const int PartSpareTapeID = 16;
        /// <summary>
        /// 底座编号
        /// </summary>
        public const int PartBaseID = 17;
       
        #endregion

        #region 配件相关常量

        public const string Parts_Supplier = "Supplier";
        public const string Parts_SpecName = "SpecName";
        public const string Parts_RecordDate = "RecordDate";
        public const string Parts_Amount = "Amount";


        public const string Parts_ObjectInfo = "objectInfo";
        #endregion
    }
}
